////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "messageinfo.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include "assert.hpp"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

MessageInfo::MessageInfo() {
	#define   ERROR(id, message) _info[id] = Info(Error  , string(message));
	#define WARNING(id, message) _info[id] = Info(Warning, string(message));
	
	ERROR("val-readable-error"  , "the value of %1% should be readable, but it is not")
	ERROR("val-writable-error"  , "the value of %1% should be writable, but it is not")
	ERROR("id-readable-error"   , "the identity of %1% should be readable, but it is not")
	ERROR("id-writable-error"   , "the identity of %1% should be writable, but it is not")
	ERROR("no-access-error"     , "%1% is no-access, but should be accessable")
	ERROR("redeclaration"       , "the symbol `%1%' has been declared for the %2% time here")
	ERROR("undeclared"          , "the symbol `%1%' has not been declared here")
	ERROR("undeclared-at-call"  , "the function `%1%' uses the symbol `%2%' which has not been declared here")
	ERROR("wrong-type"          , "%1% is of type `%2%', but should be of type `%3%'")
	ERROR("wrong-types"         , "%1% are of type `%2%', but should be of type `%3%'")
	ERROR("not-function-type"   , "this expression is used as a function, but is not of a function-type; it is of type `%1%'")
	ERROR("not-array-type"      , "this expression is used as an array, but is not of an array-type; it is of type `%1%'")
	ERROR("type-mismatch"       , "type mismatch between %1% (typed `%2%') and %3% (typed `%4%')")
	ERROR("void-vars"           , "this variable declaration is of type `void', which is not permitted")
	ERROR("void-params"         , "this parameter declaration is of type `void', which is not permitted")
	ERROR("void-array"          , "void arrays are not permitted")
	ERROR("unexpected-character", "unexpected character: `%1%'")
	ERROR("no-run-function"     , "no `int run()' function is found")
	ERROR("a-weak-f-strong"     , "for the %1% parameter: a weak pointer may not be passed by alias to a strong pointer")
	ERROR("a-strong-f-weak"     , "for the %1% parameter: a strong pointer may not be passed by alias to a weak pointer")
	ERROR("val-weak-pointer"    , "the parameter `%1%' is passed by value, so it can not be a weak pointer")
	ERROR("array-size-limit"    , "an array may not be larger than 4,294,967,295 elements")
	ERROR("weak-logical-var"    , "logical variables are neither weak nor strong pointers, declare them without the '^' token")
	ERROR("lv-not-pre"          , "logical variables may only be declared inside a function precondition")
	ERROR("lv-in-impl"          , "the symbol `%1%' is a logical var, which may only be used in assertions")
	ERROR("side-effect-error"   , "%1% has side effects, but it is not allowed to")
	ERROR("weak-val-init"       , "a weak pointer may not be initialized with a value")
	ERROR("weak-return"         , "the return-type of a function may not be or have a weak pointer")
	ERROR("by-val-array"        , "an array may not be passed by value")
	ERROR("return-array"        , "an array may not be returned")
	ERROR("array-compare"       , "arrays may not be compared in this way")
	ERROR("function-compare"    , "functions may not be compared in this way")
	ERROR("void-compare"        , "`void' typed expressions may not be compared in this way")
	
	WARNING("predictable-return"  , "%1% will always return `%2%'")
	WARNING("unterminated-comment", "unterminated comment")
	
	#undef WARNING
	#undef ERROR
}

bool MessageInfo::code_exists(const string& code) const {
	return (_info.find(code) != _info.end());
}

MessageSeverity MessageInfo::severity(const string& id) const {
	It it = _info.find(id);
	
	assert(it != _info.end());
	
	return it->second.severity;
}

const string& MessageInfo::message(const string& id) const {
	It it = _info.find(id);
	
	assert(it != _info.end());
	
	return it->second.message;
}
